<?php

namespace app\api\middleware;

use think\Request;
use think\Response;
use app\admin\controller\Admin;
class AuthCheck
{
    public function handle(Request $request, \Closure $next): Response
    {
        if (isset($_SERVER['HTTP_AUTHORIZATION'])) {
            $admin = new Admin();
            $decodeToken = $admin->decodeToken();
            $obj = (string)$decodeToken;
            $da = json_decode($obj, true);
            if (isset($da['data'])) {
                $das = $da['data'];
            } else {
                return json([
                    'message' => '登录过期，请重新登录',
                    'code'    => 401,
                ]);
            }
        }else{
                return json(['message' => '请先登录', 'code' => 401]);
            }
        return $next($request);
    }
}